﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:Log_Check
	/// </summary>
	public partial class Log_Check
	{
		public Log_Check()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "Log_Check"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Log_Check");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.Log_Check model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Log_Check(");
			strSql.Append("User_FromId,User_CurrId,User_NextId,User_FromName,User_CurrName,User_NextName,Remark,Result,DisposeTime,IsDispose,InsTime,BeforId,AfterId,ObjectId,ObjectType,IsEnd)");
			strSql.Append(" values (");
			strSql.Append("@User_FromId,@User_CurrId,@User_NextId,@User_FromName,@User_CurrName,@User_NextName,@Remark,@Result,@DisposeTime,@IsDispose,@InsTime,@BeforId,@AfterId,@ObjectId,@ObjectType,@IsEnd)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@User_FromId", SqlDbType.Int,4),
					new SqlParameter("@User_CurrId", SqlDbType.Int,4),
					new SqlParameter("@User_NextId", SqlDbType.Int,4),
					new SqlParameter("@User_FromName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_CurrName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_NextName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@Result", SqlDbType.Bit,1),
					new SqlParameter("@DisposeTime", SqlDbType.DateTime),
					new SqlParameter("@IsDispose", SqlDbType.Bit,1),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@BeforId", SqlDbType.Int,4),
					new SqlParameter("@AfterId", SqlDbType.Int,4),
					new SqlParameter("@ObjectId", SqlDbType.Int,4),
					new SqlParameter("@ObjectType", SqlDbType.Int,4),
					new SqlParameter("@IsEnd", SqlDbType.Bit,1)};
			parameters[0].Value = model.User_FromId;
			parameters[1].Value = model.User_CurrId;
			parameters[2].Value = model.User_NextId;
			parameters[3].Value = model.User_FromName;
			parameters[4].Value = model.User_CurrName;
			parameters[5].Value = model.User_NextName;
			parameters[6].Value = model.Remark;
			parameters[7].Value = model.Result;
			parameters[8].Value = model.DisposeTime;
			parameters[9].Value = model.IsDispose;
			parameters[10].Value = model.InsTime;
			parameters[11].Value = model.BeforId;
			parameters[12].Value = model.AfterId;
			parameters[13].Value = model.ObjectId;
			parameters[14].Value = model.ObjectType;
			parameters[15].Value = model.IsEnd;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.Log_Check model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Log_Check set ");
			strSql.Append("User_FromId=@User_FromId,");
			strSql.Append("User_CurrId=@User_CurrId,");
			strSql.Append("User_NextId=@User_NextId,");
			strSql.Append("User_FromName=@User_FromName,");
			strSql.Append("User_CurrName=@User_CurrName,");
			strSql.Append("User_NextName=@User_NextName,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("Result=@Result,");
			strSql.Append("DisposeTime=@DisposeTime,");
			strSql.Append("IsDispose=@IsDispose,");
			strSql.Append("InsTime=@InsTime,");
			strSql.Append("BeforId=@BeforId,");
			strSql.Append("AfterId=@AfterId,");
			strSql.Append("ObjectId=@ObjectId,");
			strSql.Append("ObjectType=@ObjectType,");
			strSql.Append("IsEnd=@IsEnd");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@User_FromId", SqlDbType.Int,4),
					new SqlParameter("@User_CurrId", SqlDbType.Int,4),
					new SqlParameter("@User_NextId", SqlDbType.Int,4),
					new SqlParameter("@User_FromName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_CurrName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_NextName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@Result", SqlDbType.Bit,1),
					new SqlParameter("@DisposeTime", SqlDbType.DateTime),
					new SqlParameter("@IsDispose", SqlDbType.Bit,1),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@BeforId", SqlDbType.Int,4),
					new SqlParameter("@AfterId", SqlDbType.Int,4),
					new SqlParameter("@ObjectId", SqlDbType.Int,4),
					new SqlParameter("@ObjectType", SqlDbType.Int,4),
					new SqlParameter("@IsEnd", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.User_FromId;
			parameters[1].Value = model.User_CurrId;
			parameters[2].Value = model.User_NextId;
			parameters[3].Value = model.User_FromName;
			parameters[4].Value = model.User_CurrName;
			parameters[5].Value = model.User_NextName;
			parameters[6].Value = model.Remark;
			parameters[7].Value = model.Result;
			parameters[8].Value = model.DisposeTime;
			parameters[9].Value = model.IsDispose;
			parameters[10].Value = model.InsTime;
			parameters[11].Value = model.BeforId;
			parameters[12].Value = model.AfterId;
			parameters[13].Value = model.ObjectId;
			parameters[14].Value = model.ObjectType;
			parameters[15].Value = model.IsEnd;
			parameters[16].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Log_Check ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Log_Check ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.Log_Check GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,User_FromId,User_CurrId,User_NextId,User_FromName,User_CurrName,User_NextName,Remark,Result,DisposeTime,IsDispose,InsTime,BeforId,AfterId,ObjectId,ObjectType,IsEnd from Log_Check ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			Model.Log_Check model=new Model.Log_Check();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["User_FromId"]!=null && ds.Tables[0].Rows[0]["User_FromId"].ToString()!="")
				{
					model.User_FromId=int.Parse(ds.Tables[0].Rows[0]["User_FromId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["User_CurrId"]!=null && ds.Tables[0].Rows[0]["User_CurrId"].ToString()!="")
				{
					model.User_CurrId=int.Parse(ds.Tables[0].Rows[0]["User_CurrId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["User_NextId"]!=null && ds.Tables[0].Rows[0]["User_NextId"].ToString()!="")
				{
					model.User_NextId=int.Parse(ds.Tables[0].Rows[0]["User_NextId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["User_FromName"]!=null && ds.Tables[0].Rows[0]["User_FromName"].ToString()!="")
				{
					model.User_FromName=ds.Tables[0].Rows[0]["User_FromName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["User_CurrName"]!=null && ds.Tables[0].Rows[0]["User_CurrName"].ToString()!="")
				{
					model.User_CurrName=ds.Tables[0].Rows[0]["User_CurrName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["User_NextName"]!=null && ds.Tables[0].Rows[0]["User_NextName"].ToString()!="")
				{
					model.User_NextName=ds.Tables[0].Rows[0]["User_NextName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark"]!=null && ds.Tables[0].Rows[0]["Remark"].ToString()!="")
				{
					model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Result"]!=null && ds.Tables[0].Rows[0]["Result"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["Result"].ToString()=="1")||(ds.Tables[0].Rows[0]["Result"].ToString().ToLower()=="true"))
					{
						model.Result=true;
					}
					else
					{
						model.Result=false;
					}
				}
				if(ds.Tables[0].Rows[0]["DisposeTime"]!=null && ds.Tables[0].Rows[0]["DisposeTime"].ToString()!="")
				{
					model.DisposeTime=DateTime.Parse(ds.Tables[0].Rows[0]["DisposeTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsDispose"]!=null && ds.Tables[0].Rows[0]["IsDispose"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsDispose"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsDispose"].ToString().ToLower()=="true"))
					{
						model.IsDispose=true;
					}
					else
					{
						model.IsDispose=false;
					}
				}
				if(ds.Tables[0].Rows[0]["InsTime"]!=null && ds.Tables[0].Rows[0]["InsTime"].ToString()!="")
				{
					model.InsTime=DateTime.Parse(ds.Tables[0].Rows[0]["InsTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["BeforId"]!=null && ds.Tables[0].Rows[0]["BeforId"].ToString()!="")
				{
					model.BeforId=int.Parse(ds.Tables[0].Rows[0]["BeforId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["AfterId"]!=null && ds.Tables[0].Rows[0]["AfterId"].ToString()!="")
				{
					model.AfterId=int.Parse(ds.Tables[0].Rows[0]["AfterId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ObjectId"]!=null && ds.Tables[0].Rows[0]["ObjectId"].ToString()!="")
				{
					model.ObjectId=int.Parse(ds.Tables[0].Rows[0]["ObjectId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ObjectType"]!=null && ds.Tables[0].Rows[0]["ObjectType"].ToString()!="")
				{
					model.ObjectType=int.Parse(ds.Tables[0].Rows[0]["ObjectType"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsEnd"]!=null && ds.Tables[0].Rows[0]["IsEnd"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsEnd"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsEnd"].ToString().ToLower()=="true"))
					{
						model.IsEnd=true;
					}
					else
					{
						model.IsEnd=false;
					}
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,User_FromId,User_CurrId,User_NextId,User_FromName,User_CurrName,User_NextName,Remark,Result,DisposeTime,IsDispose,InsTime,BeforId,AfterId,ObjectId,ObjectType,IsEnd ");
			strSql.Append(" FROM Log_Check ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,User_FromId,User_CurrId,User_NextId,User_FromName,User_CurrName,User_NextName,Remark,Result,DisposeTime,IsDispose,InsTime,BeforId,AfterId,ObjectId,ObjectType,IsEnd ");
			strSql.Append(" FROM Log_Check ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Log_Check ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Log_Check T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Log_Check";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

